package com.zhugang.week04;

import java.util.HashMap;
import java.util.Map;

/**
 * @program algorithms
 * @description: fib
 * @author: chanzhugang
 * @create: 2022/06/18 10:23
 */
public class Fib {

    /**
     * 剑指offer 10-I 斐波那契数列
     *
     * @param n
     * @return
     */
    Map<Integer, Integer> cache = new HashMap<>();
    private final int mod = 1000000007;

    public int fib(int n) {
        if (n == 0) return 0;
        if (n == 1) return 1;
        if (cache.containsKey(n)) {
            return cache.get(n);
        }
        int res = (fib(n - 1) + fib(n - 2)) % mod;
        cache.put(n, res);
        return res;
    }
}